package com.example.zbh.dsliao.ui.activty;

import android.text.TextUtils;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.easemob.EMCallBack;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.FileMessageBody;
import com.easemob.util.FileUtils;
import com.example.zbh.dsliao.R;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zbh on 15/11/28.
 */
public class ShowNomalFileActivity extends BaseActivity {
    private ProgressBar progressBar;
    private File file;
    @Override
    protected int getLayOut() {
        return R.layout.activity_show_nomal_file_activity;
    }

    @Override
    protected void initView() {
        progressBar = findeView(R.id.progressBar);



    }

    @Override
    protected void initData() {
        FileMessageBody messageBody = getIntent().getParcelableExtra("msgbody");
        file = new File(messageBody.getLocalUrl());
        Map<String, String> map = new HashMap<>();
        if (!TextUtils.isEmpty((messageBody.getSecret()))){
            map.put("share-secret", messageBody.getSecret());
        }
        EMChatManager.getInstance().downloadFile(messageBody.getRemoteUrl(), messageBody.getLocalUrl(), map, new EMCallBack() {
            @Override
            public void onSuccess() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        FileUtils.openFile(file, ShowNomalFileActivity.this);
                        finish();
                    }
                });
            }

            @Override
            public void onError(int i, String s) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (file != null && file.exists()&&file.isFile()){
                            file.delete();
                            Toast.makeText(ShowNomalFileActivity.this, "下载失败",Toast.LENGTH_SHORT).show();
                            finish();
                        }
                    }
                });
            }

            @Override
            public void onProgress(int i, String s) {
                progressBar.setProgress(i);

            }
        });

    }
}
